<template>
  <div class="button">
    <!-- <el-input
      id="input"
      v-model="$store.state.detail.count"
      @click="selectValue($event)"
      :max="999"
      :min="1"
    ></el-input> -->
    <!-- <el-input v-model="$store.state.detail.count" /> -->
    <input
      type="text"
      v-model="$store.state.detail.count"
      class="inp"
      @input="selectValue"
    />
    <!-- $store.state.detail.count = isNaN($store.state.detail.count)
          ? $store.state.detail.count.replace(/[^\d]/g, '')
          : $store.state.detail.count -->
    <div class="btn">
      <button @click="add">+</button>
      <button @click="sub">-</button>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Button',

  methods: {
    sub() {
      this.$store.commit('countChenge', 'sub')
    },
    add() {
      this.$store.commit('countChenge', 'add')
    },

    selectValue(e) {
      // 文本框只能输入数字 且不为0
      this.$store.state.detail.count = e.target.value
        .replace(/\D/g, '')
        .replace(/^0{1,}/g, '')
      // 如果为空 重置
      setTimeout(() => {
        if (this.$store.state.detail.count == '') {
          this.$store.state.detail.count = 1
        }
      }, 1000)
    },

    // 监听文本框内容
    inpChenge() {
      this.$store.commit('countChenge', 'inpChenge')
    }
  }
}
</script>

<style scoped lang="less">
.button {
  .inp {
    margin-top: 4px;
    float: left;
    width: 30px;
    height: 30px;
    border: 0;
    outline: 1px solid rgb(0, 0, 0);
    box-sizing: border-box;
    text-align: center;
  }
  .btn {
    margin-top: 4px;
    float: left;
    margin-left: 2px;
    margin-right: 6px;
    width: 25px;
    height: 30px;

    button {
      display: block;
      cursor: pointer;
      width: 15px;
      height: 15px;
    }
  }
}
</style>
